package com.LA_houseLease.admin.controller.system;

import com.LA_houseLease.admin.service.SystemUserService;
import com.LA_houseLease.admin.vo.system.user.SystemUserItemVo;
import com.LA_houseLease.admin.vo.system.user.SystemUserQueryVo;
import com.LA_houseLease.common.result.Result;
import com.LA_houseLease.pojo.entity.SystemUser;
import com.LA_houseLease.pojo.enums.BaseStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@CrossOrigin(origins = "*")
@Slf4j
@Tag(name = "后台用户信息管理")
@RestController
@RequestMapping("/admin/system/user")
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;

    @Operation(summary = "根据条件分页查询后台用户列表")
    @GetMapping("page")
    public Result<IPage<SystemUserItemVo>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVo queryVo) {
        log.warn("根据条件分页查询后台用户列表--SystemUserController.page");
        Page<SystemUserItemVo> page = new Page<>(current, size);
        IPage<SystemUserItemVo> systemUserIPage=systemUserService.SystemUserPage(page,queryVo);
        return Result.ok(systemUserIPage);
    }

    @Operation(summary = "根据ID查询后台用户信息")
    @GetMapping("getById")
    public Result<SystemUserItemVo> getById(@RequestParam Long id) {
        log.warn("根据ID查询后台用户信息--SystemUserController.getById");
        SystemUserItemVo systemUserItemVo=systemUserService.getSystemUserItemVoById(id);
        return Result.ok(systemUserItemVo);
    }

    @Operation(summary = "保存或更新后台用户信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser) {
        log.warn("保存或更新后台用户信息--SystemUserController.saveOrUpdate");
        if(systemUser.getPassword()!=null){
            //如果给加密算法填入的为null会报错
            systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
        }
        systemUserService.saveOrUpdate(systemUser);
        return Result.ok();
    }

    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) {
        log.warn("判断后台用户名是否可用--SystemUserController.isUsernameExists");
        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemUser::getUsername,username);
        long count = systemUserService.count(lambdaQueryWrapper);
        return Result.ok(count==0);
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result removeById(@RequestParam Long id) {
        log.warn("根据ID删除后台用户信息--SystemUserController.removeById");
        systemUserService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @PostMapping("updateStatusByUserId")
    public Result updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        log.warn("根据ID修改后台用户状态--SystemUserController.updateStatusByUserId");
        LambdaUpdateWrapper<SystemUser> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(SystemUser::getId,id);
        lambdaUpdateWrapper.set(SystemUser::getStatus,status);
        systemUserService.update(lambdaUpdateWrapper);
        return Result.ok();
    }
}














